﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Coding4Fun.Maelstrom.Tablet
{
	public sealed partial class Paddle : UserControl
	{
		private double _oldX = 0.0;
		private double _oldY = 0.0;
		private double _speed = 0.0;
		private double _directionTarget = 0.0;
		private double _speedAngle = 0.0;
        private double _directionCurrent = 0.0;

		public double X
		{
			set { this.SetValue(Canvas.LeftProperty, value); }
			get { return (double)this.GetValue(Canvas.LeftProperty); }
		}

		public double Y
		{
			set { this.SetValue(Canvas.TopProperty, value); }
			get { return (double)this.GetValue(Canvas.TopProperty); }
		}

		public Paddle()
		{
			this.InitializeComponent();
		}

		public void SetPosition(double _x, double _y)
		{
			_oldX = X;
			_oldY = Y;
			X = _x;
			Y = _y;
			double dx = _oldX - X;
			double dy = _oldY - Y;
			_speed = dx * dx + dy * dy;
			_speedAngle += _speed*.005;

            //double directionStep = Math.Atan2(dy, dx)*(180.0/Math.PI);
            //if (directionStep > 180.0) directionStep -= 360.0;
            //if (directionStep < -180.0) directionStep += 360.0;
            //_directionTarget += (directionStep - _directionTarget) * .1;
            //if (_directionTarget > 180.0) _directionTarget -= 360.0;
            //if (_directionTarget < -180.0) _directionTarget += 360.0; 

            _directionTarget = Math.Atan2(540.0 - Y, 960.0 - X) * (180.0 / Math.PI);


		}

		public void Update() 
		{
			speedRot.Angle = _speedAngle;
            _speedAngle *= .97;
			_directionCurrent += (_directionTarget - _directionCurrent) * .3;
			directionRot.Angle = _directionTarget;
		}
	}
}
